class Page
  
  def initialize(archive, index)
    @archive = archive
    @index = index
  end

  def path_in_archive
    @path_in_archive ||= @archive.filenames[@index.to_i]
  end

  def full_filename
    @path_from_root if @path_from_root && File.exists?(@path_from_root)
    @path_from_root = @archive.file(path_in_archive)
  end

  def public_filename
    @public_filename if @public_filename
    @public_filename = full_filename.gsub(/^\/*public\/scratch/, '')
    @public_filename = File.join(
      'scratch',
      ERB::Util.u(File.dirname(@public_filename)),
      ERB::Util.u(File.basename(@public_filename))
    )
  end

  def found?
    !(path_in_archive.blank? && full_filename.blank?)
  end
  
  def size
    @size ||= ImageSize.new(IO.read(full_filename))
  end
  
  def width
    size.get_width
  end

  def height
    size.get_height
  end

end
